
namespace FastFood.DataAccess
{
	using System;
	using System.Collections.Generic;
	using System.Data;
	using System.Data.Common;
	using Microsoft.Practices.EnterpriseLibrary.Data;
	using EDOSwit.Entity;
    using FastFood.Business.Entities;

	/// <summary>
	/// Represents the DataAccess of CombosData.
	/// </summary>
	public class CombosData : Singleton<CombosData>
	{
		#region Attributes

		private readonly Database BaseDatos = DatabaseFactory.CreateDatabase();

		#endregion

		#region Methods

		/// <summary>
		/// Selects all records from the Combos table by a foreign key.
		/// </summary>
		public IList<Combos> Listar()
		{
			var entidades = new List<Combos>();
			var comando = BaseDatos.GetStoredProcCommand("SelectAllCombos");

			using (var lector = BaseDatos.ExecuteReader(comando))
			{
				while (lector.Read())
				{
					entidades.Add(new Combos
					{
						comboID = lector.IsDBNull(lector.GetOrdinal("comboID"))? default(short) : lector.GetInt16(lector.GetOrdinal("comboID")),
						nombre = lector.IsDBNull(lector.GetOrdinal("nombre"))? default(string) : lector.GetString(lector.GetOrdinal("nombre")),
						precio = lector.IsDBNull(lector.GetOrdinal("precio"))? default(decimal) : lector.GetDecimal(lector.GetOrdinal("precio")),
						descripcion = lector.IsDBNull(lector.GetOrdinal("descripcion"))? default(string) : lector.GetString(lector.GetOrdinal("descripcion"))
					});
				}
			}
			comando.Dispose();
			return entidades;
		}

		/// <summary>
		/// Selects all records from the Combos table by a foreign key.
		/// </summary>
		public Combos Obtener(short comboID)
		{
			var combos = default(Combos);
			var comando = BaseDatos.GetStoredProcCommand("SelectCombos");

			BaseDatos.AddInParameter(comando, "comboID", DbType.Int16, comboID);

			using (var lector = BaseDatos.ExecuteReader(comando))
			{
				while (lector.Read())
				{
					combos = new Combos
					{
						comboID = lector.IsDBNull(lector.GetOrdinal("comboID"))? default(short) : lector.GetInt16(lector.GetOrdinal("comboID")),
						nombre = lector.IsDBNull(lector.GetOrdinal("nombre"))? default(string) : lector.GetString(lector.GetOrdinal("nombre")),
						precio = lector.IsDBNull(lector.GetOrdinal("precio"))? default(decimal) : lector.GetDecimal(lector.GetOrdinal("precio")),
						descripcion = lector.IsDBNull(lector.GetOrdinal("descripcion"))? default(string) : lector.GetString(lector.GetOrdinal("descripcion"))
					};
				}
			}
			comando.Dispose();
			return combos;
		}

		/// <summary>
		/// Saves a record to the Combos table.
		/// </summary>
		public short AgregarBase(Combos combos, DbTransaction transaccion)
		{
			var comando = BaseDatos.GetStoredProcCommand("InsertCombos");
			BaseDatos.AddOutParameter(comando, "comboID", DbType.Int16, 2);
			BaseDatos.AddInParameter(comando, "nombre", DbType.String, combos.nombre);
			BaseDatos.AddInParameter(comando, "precio", DbType.Currency, combos.precio);
			BaseDatos.AddInParameter(comando, "descripcion", DbType.String, combos.descripcion);

			var resultado = BaseDatos.ExecuteNonQuery(comando, transaccion);
            if (resultado == 0) throw new Exception("Error al Agregar Combos.");

            var valorDevuelto = (short)BaseDatos.GetParameterValue(comando, "comboID");
			comando.Dispose();
            return valorDevuelto;
		}

		/// <summary>
		/// Updates a record in the Combos table.
		/// </summary>
        public bool ModificarBase(Combos combos, DbTransaction transaccion)
		{
			var comando = BaseDatos.GetStoredProcCommand("UpdateCombos");
			BaseDatos.AddInParameter(comando, "comboID", DbType.Int16, combos.comboID);
			BaseDatos.AddInParameter(comando, "nombre", DbType.String, combos.nombre);
			BaseDatos.AddInParameter(comando, "precio", DbType.Currency, combos.precio);
			BaseDatos.AddInParameter(comando, "descripcion", DbType.String, combos.descripcion);

			var resultado = BaseDatos.ExecuteNonQuery(comando, transaccion);
			if (resultado == 0) throw new Exception("Error al Modificar Combos.");

			comando.Dispose();
			return true;
		}

        /// <summary>
        /// Saves a record to the Productos table.
        /// </summary>
        public short Agregar(Combos combo)
        {
            short comboID;
            using (DbConnection conexion = BaseDatos.CreateConnection())
            {
                conexion.Open();
                DbTransaction transaccion = conexion.BeginTransaction();

                try
                {
                    comboID = this.AgregarBase(combo, transaccion);
                    foreach (Combos_Insumos detalle in combo.Combos_Insumos)
                    {
                        Combos_InsumosData.Instancia.Agregar(comboID, detalle, transaccion);
                    }
                    foreach (Combos_Productos detalle in combo.Combos_Productos)
                    {
                        Combos_ProductosData.Instancia.Agregar(comboID, detalle, transaccion);
                    }
                    transaccion.Commit();
                }
                catch (Exception)
                {
                    transaccion.Rollback();
                    throw;
                }
                conexion.Close();
            }
            return comboID;
        }

        /// <summary>
        /// Updates a record in the Combos table.
        /// </summary>
        public bool Modificar(Combos combo)
        {
            using (DbConnection conexion = BaseDatos.CreateConnection())
            {
                conexion.Open();
                DbTransaction transaccion = conexion.BeginTransaction();

                try
                {
                    this.ModificarBase(combo, transaccion);
                    Combos_InsumosData.Instancia.EliminarPorComboID(combo.comboID, transaccion);
                    Combos_ProductosData.Instancia.EliminarPorComboID(combo.comboID, transaccion);

                    foreach (Combos_Insumos detalle in combo.Combos_Insumos)
                    {
                        Combos_InsumosData.Instancia.Agregar(combo.comboID, detalle, transaccion);
                    }
                    foreach (Combos_Productos detalle in combo.Combos_Productos)
                    {
                        Combos_ProductosData.Instancia.Agregar(combo.comboID, detalle, transaccion);
                    }
                    transaccion.Commit();
                }
                catch (Exception)
                {
                    transaccion.Rollback();
                    throw;
                }
                conexion.Close();
            }
            return true;
        }

        /// <summary>
        /// Deletes a record from the Combos table by its primary key.
        /// </summary>
        public bool Eliminar(short comboID)
        {
            var comando = BaseDatos.GetStoredProcCommand("DeleteCombos");
            BaseDatos.AddInParameter(comando, "comboID", DbType.Int16, comboID);

            var resultado = BaseDatos.ExecuteNonQuery(comando);
            if (resultado == 0) throw new Exception("Error al Eliminar Combos.");
            comando.Dispose();
            return true;
        }

		#endregion
	}
}
